import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'app/routes/app_pages.dart';
import 'package:flutter/services.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'utils/common/langs/index.dart';
import 'utils/common/styles/theme.dart';


class MyApp extends StatelessWidget with WidgetsBindingObserver {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetxApp(
      // 设计稿尺寸 单位：dp
      designSize: const Size(390, 844),
      enableLog: kDebugMode, // Getx Log
      defaultTransition: Transition.rightToLeft, // 默认的跳转动画
      themeMode: GlobalService.to.themeMode, // 主题模式
      theme: AppTheme.light, // 主题
      darkTheme: AppTheme.dark, // Dark主题
      debugShowCheckedModeBanner: false, // 国际化配置
      locale: GlobalService.to.locale,
      translations: TranslationLibrary(),
      fallbackLocale: TranslationLibrary.fallbackLocale,
      supportedLocales: TranslationLibrary.supportedLocales,
      localizationsDelegates: TranslationLibrary.localizationsDelegates,
      // AppTitle
      title: 'Bkm',
      // 首页
      // home: const HomePage(),
      getPages: AppPages.routes,
      initialRoute: AppPages.INITIAL,
      // Builder
      builder: (context, widget) {
        // do something....
        GlobalService.to.updateNavigationBar(context);
        return FlutterSmartDialog.init()(context, widget);
      },
    );
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive:
        debugPrint("处在不活动状态，无法处理用户响应");
        break;

      case AppLifecycleState.resumed:
        debugPrint("应用程序前台可见，且能响应用户的输入");
        // AppLaunching.checkAppVersionUpdate();
        break;

      case AppLifecycleState.paused:
        debugPrint("应用程序不可见，不能响应用户的输入，且在后台继续活动中");
        break;

      case AppLifecycleState.detached:
        break;
      case AppLifecycleState.hidden:
        break;
    }
  }
}






class NetworkListener with WidgetsBindingObserver {
  // 创建一个 Connectivity 实例用于检查网络连接状态
  static final Connectivity _connectivity = Connectivity();
  // 存储当前的网络连接状态
  static List<ConnectivityResult> _connectionStatus = [];
  // 声明一个 StreamSubscription 用于监听网络状态变化
  static late StreamSubscription<List<ConnectivityResult>> _subscription;

  // 私有构造函数
  NetworkListener._internal();

  // 单例实例
  static final NetworkListener _instance = NetworkListener._internal();
  // 提供一个静态 getter 来获取单例实例
  static NetworkListener get instance => _instance;
  // 提供一个工厂构造函数返回单例实例
  factory NetworkListener() => _instance;

  Future<void> start() async {
    WidgetsBinding.instance.addObserver(_instance);

    try {
      // 初始化网络状态
      _connectionStatus = await _connectivity.checkConnectivity();
    } on PlatformException catch (e) {
      debugPrint('check connectivity status = ${e.toString()}');
      return;
    }
    // 监听网络状态变化
    _subscription = _connectivity.onConnectivityChanged.listen((result) {
      _connectionStatus = result;
      _updateConnectionStatus();
    });
  }

  void dispose() {
    _subscription.cancel();
    WidgetsBinding.instance.removeObserver(this as WidgetsBindingObserver);
  }

  /// 监听网络变化后的回调
  static Future<void> _updateConnectionStatus() async {
    debugPrint('当前网络 Connectivity changed: $_connectionStatus');
    switch (_connectionStatus.first) {
      case ConnectivityResult.wifi:
        debugPrint('****** 当前网络 ******');
        debugPrint('****** WIFI ******');
        debugPrint('****** 当前网络 ******');
        break;
      case ConnectivityResult.mobile:
        debugPrint('****** 当前网络 ******');
        debugPrint('****** MOBILE ******');
        debugPrint('****** 当前网络 ******');
        break;
      case ConnectivityResult.none:
      // showMessage('无网络，请检查网络设置');
        break;
      default:
      // showMessage('网络连接失败，请检查网络！');
        break;
    }
  }
}



